﻿using FCT551.Models;
using FCT551.Procedure;
using NewDevice.RS232;
using NewDevice.RS485;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace FCT551.Helper
{
    /// <summary>
    /// 所有的全局变量，全部放在这里
    /// </summary>
    public static class Global

    {
        /// <summary>
        /// 所有的配置文件
        /// </summary>
        public static List<Config> _Configs;

        /// <summary>
        /// 加载的用户配置
        /// </summary>
        public static List<User> _Users;

        /// <summary>
        /// 当前选择的配置
        /// </summary>
        public static Config? _Config;

        /// <summary>
        /// 系统是否初始化
        /// </summary>
        public static bool _isInit = false;

        /// <summary>
        /// 当前登录的用户
        /// </summary>
        public static User _User;

        /// <summary>
        /// xml文件配置路径
        /// </summary>
        public static string _ConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml");

        /// <summary>
        /// 系统配置文件中每一个sheet的名字
        /// </summary>
        public static string _IOconfigName = "IO定义";

        public static string _ProduceConfigName = "产品配置";
        public static string _TestProcedureconfigName = "测试流程";
        public static string _TestMethodsconfigName = "测试方法";
        public static string _TestconfigName = "测试配置";

        /// <summary>
        /// 存放系统所有IO配置
        /// </summary>
        public static Dictionary<string, string>? _IOconfigs;

        /// <summary>
        /// 系统产品配置
        /// </summary>
        public static List<ProduceConfigModel> _ProduceConfigs;

        /// <summary>
        /// 所有确定执行的工位id，用来打开io
        /// </summary>
        public static List<int> ExeStationsIDs = new List<int>();

        /// <summary>
        /// 存放系统所有测试配置。就是补偿
        /// </summary>
        public static Dictionary<string, int>? _TestConfigs;

        /// <summary>
        /// 存在系统所有测试流程
        /// </summary>
        public static List<TestMethodModel>? _TestProcedureConfigs;

        /// <summary>
        /// 测试方法以及IO配置等路径
        /// </summary>
        public static string _SysConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sysconfig.xlsx");

        /// <summary>
        /// 当前用户是否登录
        /// </summary>
        public static bool _isLogin = false;

        /// <summary>
        /// 数据库客户端
        /// </summary>
        public static SqlSugarClient? _sqlSugarClient;

        /// <summary>
        /// 客户端
        /// </summary>
        public static SocketClient client = new SocketClient("192.168.25.11", 8888);

        //客户端链接状态
        public static bool ClientISConnect = false;

        public static bool IsFirstStationExe = true;

        /// <summary>
        /// 读取所有设置方法
        /// </summary>
        //public static List<Action<TestMethodModel>> TestSetMethodModelActions;
        public static Dictionary<string, Action<TestMethodModel>> TestSetMethodModelActions;

        /// <summary>
        /// 读取所有读取方法
        /// </summary>
        //public static List<Func<TestMethodModel, bool>> TestGetMethodModelActions;
        public static Dictionary<string, Func<TestMethodModel, bool>> TestGetMethodModelActions;

        public static void ClearConfig()
        {
        }

        public static readonly object SqlSugarLock = new object();

        /// <summary>
        /// 每天生成的产能表初始数据
        /// </summary>
        public static List<CapacityData> _CapacityDatas = new List<CapacityData>()
        {
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "00:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "01:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "02:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "03:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "04:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "05:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "06:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "07:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "08:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "09:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "10:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "11:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "12:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "13:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "14:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "15:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "16:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "17:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "18:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "19:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "20:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "21:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "22:00"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "23:00"},
        };

        /// <summary>
        /// 年产能表初始数据
        /// </summary>
        public static List<CapacityData> _CapacityDatasByYear = new List<CapacityData>()
        {
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "1"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "2"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "3"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "4"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "5"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "6"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "7"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "8"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "9"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "10"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "11"},
            new CapacityData(){NGnums = 0,OKnums = 0,Time = "12"},
        };

        /// <summary>
        /// 程控电源
        /// </summary>
        public static IT6302 _It6302;

        /// <summary>
        /// 所有的采集模块
        /// </summary>
        public static List<ZS_AnalogInput> _ZsAnalogInputs;

        /// <summary>
        /// 单路电源
        /// </summary>
        public static RU_12_3040 _RU_12_3040;

        /// <summary>
        /// 16路PWM模块
        /// </summary>
        public static QjxPwm16 _qjxPwm16;

        /// <summary>
        /// 4路PWM模块
        /// </summary>
        public static QjxPwm4 _qjxPwm4;

        /// <summary>
        /// 通用IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoCommon;

        /// <summary>
        /// 英鹏飞电机1，2
        /// </summary>
        public static YPF_40R _YPF_Y40R_1;

        public static YPF_40R _YPF_Y40R_2;

        /// <summary>
        /// 电流采集模块
        /// </summary>
        public static ZH_44242G_14F2 _ZH44242G14F2_Current;

        /// <summary>
        /// 电压采集模块
        /// </summary>
        public static ZH_44242G_14F2 _ZH44242G14F2_Voltage;

        /// <summary>
        /// 3.3V IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoThreePointThree;

        /// <summary>
        /// 24V IO采集模块
        /// </summary>
        public static PQW_IO _PqwIoTwoFour;

        /// <summary>
        /// 所有采集模块的24V数据
        /// </summary>
        public static Dictionary<int, List<double>> _allZsAnalogOutputsDatas24 = new();

        /// <summary>
        /// 所有采集模块的3.3V数据
        /// </summary>
        public static Dictionary<int, List<double>> _allZsAnalogOutputsDatas33 = new();

        /// <summary>
        /// 所有工位的数据
        /// </summary>
        public static Dictionary<int, List<double>> _allWorkStationOutputsDatas = new();

        /// <summary>
        /// 当前是否是io调试状态
        /// </summary>
        public static bool _isIOdebug = false;

        /// <summary>
        /// 当前工站名称
        /// </summary>
        public static string _WorkStationName = "";

        /// <summary>
        /// 当前选择工单
        /// </summary>
        public static string _WorkStationOrderName = "";

        /// <summary>
        /// 当前机台是否在运行
        /// </summary>
        public static bool _isRunning = false;

        public static MESServiceClient _MESServiceClient = new();

        /// <summary>
        /// 当前机台是否在急停
        /// </summary>
        public static bool _isEmg = false;

        /// <summary>
        /// 证书是否过期
        /// </summary>
        public static bool _Lisence = false;

        public static CancellationTokenSource _EmgStopTokenSource = new CancellationTokenSource();

        /// <summary>
        /// 当前是否是单步测试
        /// </summary>
        public static bool isOntStep = false;

        /// <summary>
        /// 是否是第一次手动
        /// </summary>
        public static bool isFirstOnStepExe = true;

        /// <summary>
        /// 手动释放全局变量里的非托管资源
        /// </summary>
        public static void Dispose()
        {
            Global.client?.close();
            Global._qjxPwm16?.SetALLStation(100);
            Global._qjxPwm4?.SetALLStation(100);
            _YPF_Y40R_1?.Stop();
            _YPF_Y40R_2?.Stop();
            //_ZH44242G14F2_Current?.Disconnect();
            //_ZH44242G14F2_Voltage?.Disconnect();
            _YPF_Y40R_1?.Dispose();
            _YPF_Y40R_2?.Dispose();
            _qjxPwm4?.Close();
            _qjxPwm16?.Close();
            _RU_12_3040?.Close();
            _sqlSugarClient?.Dispose();
        }

        public static List<Config>? Clone(this List<Config>? obj)
        {
            List<Config> newConfigs = new();
            foreach (var item in obj)
            {
                newConfigs.Add(item.Clone() as Config);
            }

            return newConfigs;
        }
    }
}